<?php
    require_once '../../common/DBHelper.php';
    // 获取从前端传递回来的当前页码和每页显示的条数
    $currentPage = $_POST['currentPage'];
    $pageSize = $_POST['pageSize'];
    
    // 算出从哪里开始获取
    $offset = ($currentPage - 1 ) * $pageSize;

    $conn = conn();
    $sql = "SELECT comments.*,posts.title FROM comments
    LEFT JOIN posts ON comments.post_id=posts.id
    LIMIT {$offset},{$pageSize}";
    $res = query($conn,$sql);
    // 需要给前端返回一个数据的总条数，让前端做分页处理
    $sqlCt = "SELECT COUNT(*) as total FROM comments";
    $countRes = query($conn,$sqlCt);
    $count = $countRes[0]['total'];
    
    // 返回
    $response = ['code' => 0,'msg' => '操作失败'];
    if($res){
        $response['code'] = 1;
        $response['msg'] = '操作成功';
        $response['data'] = $res;
        $response['dataCount'] = $count;
        }
    close($conn);
    echo json_encode($response,JSON_UNESCAPED_UNICODE);
?>